package com.audaque.springboot.foshanupload.web.amqpdemo.controller;

import cn.hutool.json.JSONUtil;
import com.audaque.springboot.foshanupload.web.amqpdemo.model.rsp.SysUserMpgtRsp;
import com.rabbitmq.client.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeoutException;

@Slf4j
@RestController
@RequestMapping("/producerClient")
public class ProducerClientController {

    @Autowired
    private ConnectionFactory connectionFactory;
    @PostMapping("/helloworld")
    public void helloworld( //路由键：队列的key
                                   @RequestParam(value = "queue", required = false) String queue,
                                   @RequestBody SysUserMpgtRsp sysUserMpgtRs) throws IOException, TimeoutException {
        Connection connection = connectionFactory.newConnection();
        //创建通道
        Channel channel = connection.createChannel();
        //参数1: 是否持久化  参数2:是否独占队列 参数3:是否自动删除  参数4:其他属性
        channel.queueDeclare(queue,true,false,false,null);
        String jsonStr = JSONUtil.toJsonStr(sysUserMpgtRs);
        channel.basicPublish("",queue, null,jsonStr.getBytes());
        channel.close();
        connection.close();
    }





    @PostMapping("/workQuene")
    public void workQueneSend( //路由键：队列的key
                                @RequestParam(value = "queue", required = false) String queue,
                                @RequestBody SysUserMpgtRsp sysUserMpgtRs) throws IOException, TimeoutException {
        Connection connection = connectionFactory.newConnection();
        //创建通道
        Channel channel = connection.createChannel();
        //参数1: 是否持久化  参数2:是否独占队列 参数3:是否自动删除  参数4:其他属性
        channel.queueDeclare(queue, true, false, false, null);
        for (int i = 0; i < 10; i++) {
            String jsonStr = JSONUtil.toJsonStr(sysUserMpgtRs);
            channel.basicPublish("", queue, null, (i+jsonStr).getBytes());
        }
        channel.close();
        connection.close();
    }

    @PostMapping("/fanout")
    public void fanout(@RequestParam(value = "exchange", required = false) String exchange,
                                @RequestBody SysUserMpgtRsp sysUserMpgtRs) throws IOException, TimeoutException {
        Connection connection = connectionFactory.newConnection();
        //创建通道
        Channel channel = connection.createChannel();
        //声明交换机
        channel.exchangeDeclare(exchange,"fanout");//广播 一条消息多个消费者同时消费
        //发布消息
        String jsonStr = JSONUtil.toJsonStr(sysUserMpgtRs);
        channel.basicPublish(exchange,"",null,jsonStr.getBytes());
        channel.close();
        connection.close();
    }



    @PostMapping("/direct")
    public void direct(@RequestParam(value = "exchange", required = false) String exchange,
                       //路由键：队列的key
                                @RequestParam(value = "routingKey", required = false) String routingKey,
                                @RequestBody SysUserMpgtRsp sysUserMpgtRs) throws IOException, TimeoutException {
        Connection connection = connectionFactory.newConnection();
        //创建通道
        Channel channel = connection.createChannel();
        //声明交换机  参数1:交换机名称 参数2:交换机类型 基于指令的Routing key转发
        channel.exchangeDeclare(exchange,"direct");
        //发布消息
        String jsonStr = JSONUtil.toJsonStr(sysUserMpgtRs);
        channel.basicPublish(exchange,routingKey,null,jsonStr.getBytes());
        channel.close();
        connection.close();
    }


    @PostMapping("/topic")
    public void topic(@RequestParam(value = "exchange", required = false) String exchange,
                       //路由键：队列的key
                                @RequestParam(value = "routingKey", required = false) String routingKey,
                                @RequestBody SysUserMpgtRsp sysUserMpgtRs) throws IOException, TimeoutException {
        Connection connection = connectionFactory.newConnection();
        //创建通道
        Channel channel = connection.createChannel();
        //声明交换机  参数1:交换机名称 参数2:交换机类型 基于指令的Routing key转发
        channel.exchangeDeclare(exchange,"topic");
        //发布消息
        String jsonStr = JSONUtil.toJsonStr(sysUserMpgtRs);
        channel.basicPublish(exchange,routingKey,null,jsonStr.getBytes());
        channel.close();
        connection.close();
    }


 @PostMapping("/ttl")
    public void ttl(@RequestParam(value = "exchange", required = false) String exchange,
                       //路由键：队列的key
                                @RequestParam(value = "routingKey", required = false) String routingKey,
                                @RequestBody SysUserMpgtRsp sysUserMpgtRs) throws IOException, TimeoutException {
        Connection connection = connectionFactory.newConnection();
        //创建通道
        Channel channel = connection.createChannel();
        //声明交换机  参数1:交换机名称 参数2:交换机类型 基于指令的Routing key转发
        channel.exchangeDeclare(exchange,"topic");
        //发布消息
        String jsonStr = JSONUtil.toJsonStr(sysUserMpgtRs);
     /**
      * deliverMode 设置为 2 的时候代表持久化消息
      * expiration 意思是设置消息的有效期，超过10秒没有被消费者接收后会被自动删除
      * headers 自定义的一些属性
      * */
     //5. 发送
     Map<String, Object> headers = new HashMap<String, Object>();
     headers.put("myhead1", "111");
     headers.put("myhead2", "222");

     AMQP.BasicProperties properties = new AMQP.BasicProperties().builder()
             .deliveryMode(2)
             .contentEncoding("UTF-8")
             .expiration("100000")
             .headers(headers)
             .build();
        channel.basicPublish(exchange,routingKey,properties,jsonStr.getBytes());
        channel.close();
        connection.close();
    }






}
